import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { ConnectedRouter } from 'react-router-redux'
import { createHashHistory } from 'history'

// 引入默认全局样式
import '@alifd/next/reset.scss'

// 引入基础配置文件
import router from './router'
import configureStore from './configureStore'
import LanguageProvider from './components/LocaleProvider'
import { getLocale } from './utils/locale'
import ajax from './api/userInfo'
import { userStore } from './utils/userStore'

const initialState = {}
const history = createHashHistory()
const store = configureStore(initialState, history)
const locale = getLocale()
const ICE_CONTAINER = document.getElementById('ice-container')

if (!ICE_CONTAINER) {
  throw new Error('当前页面不存在 <div id="ice-container"></div> 节点.')
}
/* global ENV */
console.log(ENV, '当前环境变量')
// 请求得到页面的权限操作
let isShowPage = false

if (Object.keys(userStore.userInfo).length === 0) {
  ajax.getUserInfo({}).then(res => {
    render()
  }).catch(() => {
    userStore.authorities = ['test']
    isShowPage = true
    render()
  })
} else {
  isShowPage = true
  render()
}

function render () {
  ReactDOM.render(
    <LanguageProvider locale={locale}>
      <Provider store={store}>
        {
          isShowPage
            ? <ConnectedRouter history={history}>{router()}</ConnectedRouter>
            : <div/>
        }
      </Provider>
    </LanguageProvider>,
    ICE_CONTAINER
  )
}
